package com.student.dao.impl;
//@Classname MedalInfoDaoImpl
//@time 17:13
//OMEN

import com.student.dao.MedalInfoDao;
import com.student.entity.AdminInfo;
import com.student.entity.Country;
import com.student.entity.MedalInfo;
import com.student.util.DBUtil;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MedalInfoDaoImpl implements MedalInfoDao {
    @Override
    public int insert(MedalInfo entity) throws Exception {
        String sql = "insert  into medal_info(medal_id,sporting_id,participan_id,medal_ranking,medal_grade,medal_date)values (?,?,?,?,?,?)";
        try (
                Connection con = DBUtil.getConnection();
                PreparedStatement pstm = con.prepareStatement(sql);
        ) {
            pstm.setObject(1, entity.getMedalId());
            pstm.setObject(2, entity.getSportingId());
            pstm.setObject(3, entity.getParticipanId());
            pstm.setObject(4, entity.getMedalRanking());
            pstm.setObject(5, entity.getMedalGrade());
            pstm.setObject(5, entity.getMedalDate());
            return pstm.executeUpdate();

        }
    }

    @Override
    public int updateById(MedalInfo entity) throws Exception {
        String sql = "update medal_info set sporting_id=?,participan_id=?,medal_ranking=?,medal_grade=?,medal_date=?  where medal_id=?";
        try (
                Connection con = DBUtil.getConnection();
                PreparedStatement pstm = con.prepareStatement(sql);

        ) {
            pstm.setObject(1, entity.getSportingId());
            pstm.setObject(2, entity.getParticipanId());
            pstm.setObject(3, entity.getMedalRanking());
            pstm.setObject(4, entity.getMedalGrade());
            pstm.setObject(5, entity.getMedalDate());
            pstm.setObject(6, entity.getMedalId());
            return pstm.executeUpdate();

        }
    }

    @Override
    public int deleteById(Serializable id) throws Exception {
        String sql = "delete from medal_info where medal_id=?";
        try (
                Connection con = DBUtil.getConnection();
                PreparedStatement pstm = con.prepareStatement(sql);

        ) {
            pstm.setObject(1, id);
            return pstm.executeUpdate();
        }
    }

    @Override
    public MedalInfo selectById(Serializable id) throws SQLException {
        String sql = "select * from medal_info where medal_id=?";
        Connection con = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
            con = DBUtil.getConnection();
            pstm = con.prepareStatement(sql);
            pstm.setObject(1, id);
            rs = pstm.executeQuery();
            if (rs.next()) {
                MedalInfo mi=new MedalInfo();
                mi.setMedalId(rs.getInt("medal_id"));
                mi.setSportingId(rs.getInt("sporting_id"));
                mi.setParticipanId(rs.getInt("participan_id"));
                mi.setMedalRanking(rs.getInt("medal_ranking"));
                mi.setMedalGrade(rs.getString("medal_grade"));
                mi.setMedalDate(rs.getString("medal_date"));


                return mi;

            } else {
                throw new SQLException("没有数据");
            }


        } finally {
            DBUtil.close(con, pstm, rs);

        }
    }

    @Override
    public List<MedalInfo> select(int index, int len) throws SQLException {
        String sql ="select * from medal_info order by medal_id limit ?,?";
        Connection con=null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        try {
            con=DBUtil.getConnection();
            pstm=con.prepareStatement(sql);
            pstm.setObject(1,index);
            pstm.setObject(2,len);
            rs=pstm.executeQuery();
            List<MedalInfo> list=new ArrayList<>();
            while (rs.next()){
                MedalInfo mi=new MedalInfo();
                mi.setMedalId(rs.getInt("medal_id"));
                mi.setSportingId(rs.getInt("sporting_id"));
                mi.setParticipanId(rs.getInt("participan_id"));
                mi.setMedalRanking(rs.getInt("medal_ranking"));
                mi.setMedalGrade(rs.getString("medal_grade"));
                mi.setMedalDate(rs.getString("medal_date"));
                list.add(mi);
            }
            return list;

        }finally {
            DBUtil.close(con,pstm,rs);
        }
    }

    @Override
    public long count() throws SQLException {
        return 0;
    }
}
